from django.db import models
from categoriesapp.models import Category
from collectionsapp.models import Collection

class Problem(models.Model):
    action = models.CharField(max_length=200)
    object = models.CharField(max_length=200)
    complement = models.CharField(max_length=200, null=True)
    
    def __unicode__(self):
        return self.action
    
    def __str__(self):
        if self.complement:
            return "%s, %s, %s" % (self.action, self.object, self.complement)
        else:
            return "%s, %s" % (self.action, self.object)

    class Meta:
        db_table = 'Problem'

class ProblemContext(models.Model):
    context = models.TextField(null=True)
    problem = models.ForeignKey(Problem)
    UIDP = models.ForeignKey("UIDP")
    
    def __unicode__(self):
        return self.context
    
    def __str__(self):
        return "%s" % self.context

    class Meta:
        db_table = 'Context'
    
class UIDP(models.Model):
    strid = models.CharField(max_length=200)
    name = models.CharField(max_length=200) 
    #context = models.TextField(null=True)
    rationale = models.TextField(null=True)
    author = models.CharField(max_length=200)
    literature = models.CharField(max_length=300)    
    
    categories  = models.ManyToManyField(Category, db_table="UIDPCategories")
    collections = models.ManyToManyField(Collection, db_table="CollectionUIDPs")
    problem     = models.ManyToManyField(Problem, through="ProblemContext")
    
    def __unicode__(self):
        return self.name
    
    def __str__(self):
        return self.name

    class Meta:
        db_table = 'UIDPs'
        
class UIDPSynonym(models.Model):
    synonym = models.CharField(max_length=200)
    uidp = models.ForeignKey(UIDP)
    
    def __unicode__(self):
        return self.synonym
    
    def __str__(self):
        return self.synonym

    class Meta:
        db_table = 'UIDPSynonyms'

    
class UIDPSolutions(models.Model):
    solution = models.TextField()
    solution_version = models.CharField(max_length=15)
    target_version = models.CharField(max_length=15)
    
    collection = models.ForeignKey(Collection)
    uidp = models.ForeignKey(UIDP)
    
    def __unicode__(self):
        return self.link
    
    def __str__(self):
        return self.link

    class Meta:
        db_table = 'UIDPSolutions'
    
class UIDPImplementationExample(models.Model):
    name = models.CharField(max_length=200)
    link = models.CharField(max_length=200)
    description = models.TextField()
    target_version = models.CharField(max_length=15)
    
    uidp = models.ForeignKey(UIDP)
    collection = models.ForeignKey(Collection)
                    
    def __unicode__(self):
        return self.link
    
    def __str__(self):
        return self.link

    class Meta:
        db_table = 'UIDPImplementation'
        
class ImageExample(models.Model):
    name = models.CharField(max_length=200)
    image_link = models.CharField(max_length=200)
    parent_name = models.CharField(max_length=200)
    parent_link = models.CharField(max_length=200)
    description = models.TextField()
    target_version = models.CharField(max_length=15)
    collection = models.ForeignKey(Collection)
    
    def __unicode__(self):
        return self.link
    
    def __str__(self):
        return self.link

    class Meta:
        db_table = 'ImageExample'
    
class UIDPImageExample(models.Model):    
    uidp = models.ForeignKey(UIDP)
    image = models.ForeignKey(ImageExample, related_name="image")
                    
    def __unicode__(self):
        return self.uidp
    
    def __str__(self):
        return self.uidp

    class Meta:
        db_table = 'UIDPImageExample'
        
class RelationshipType(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name
    
    def __str__(self):
        return self.name

    class Meta:
        db_table = 'RelationshipTypes'
    
            
class Relationship(models.Model):
    target = models.ForeignKey(UIDP, related_name="target")
    source = models.ForeignKey(UIDP, related_name="source")
    collection = models.ForeignKey(Collection, related_name="target_collection")
    relationship_type = models.ForeignKey(RelationshipType, related_name="relationship_type")
    description = models.TextField()
    
    class Meta:
        db_table = 'Relationships'

# class EnhancementRel(Relationship):
    # description = models.TextField(null=True)
#     
    # def __unicode__(self):
        # return self.link
#     
    # def __str__(self):
        # return self.link
# 
    # class Meta:
        # db_table = 'EnhancementRel'
# 
# class SimilarityRel(Relationship):
    # description = models.TextField(null=True)
# 
    # def __unicode__(self):
        # return self.link
#     
    # def __str__(self):
        # return self.link
# 
    # class Meta:
        # db_table = 'SimilarityRel'
# 
# class UsedWithRel(Relationship):
    # def __unicode__(self):
        # return self.link
#     
    # def __str__(self):
        # return self.link
# 
    # class Meta:
        # db_table = 'UsedWithRel'
#         